package com.example.tbtx.tbtx;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tbtx.tbtx.adapter.MainPagerAdapter;
import com.example.tbtx.tbtx.adapter.ZhyBaseRecycleAdapter.CommonAdapter;
import com.example.tbtx.tbtx.adapter.ZhyBaseRecycleAdapter.MultiItemTypeAdapter;
import com.example.tbtx.tbtx.adapter.ZhyBaseRecycleAdapter.base.ViewHolder;
import com.example.tbtx.tbtx.base.BaseResponse;
import com.example.tbtx.tbtx.module.CategoryResponse;
import com.example.tbtx.tbtx.module.CheckVersionResponse;
import com.example.tbtx.tbtx.module.Father;
import com.example.tbtx.tbtx.module.Goods;
import com.example.tbtx.tbtx.module.MainListResponse;
import com.example.tbtx.tbtx.module.Son;
import com.example.tbtx.tbtx.retrofit.RetrofitHelper;
import com.example.tbtx.tbtx.utils.system.SystemUtil;
import com.example.tbtx.tbtx.view.CloudProgressView;
import com.example.tbtx.tbtx.view.ScaleInTransformer;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.Permission;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    private ViewPager cardViewPager;
    private ArrayList<MainListResponse.DatasBean> strings;

    View ll_container;
    private MainPagerAdapter adapter;
    private RecyclerView tagFlowLayout;
    private ArrayList<CategoryResponse.DatasBean> tagList = new ArrayList<>();
    private CategoryResponse.DatasBean currentTag;
    private TextView tv_name;
    private CommonAdapter<CategoryResponse.DatasBean> commonAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tagList.add(new CategoryResponse.DatasBean());
        init();
        loadData();
        String deviceID = SystemUtil.getDeviceID(this);
        int[] ints = new int[]{1, 6, 6, 6, 8, 9, 6};
        Log.d("fsssss", "onCreate: " + binarySearch(ints, 7));

    }

    public int binarySearch(int[] ints, int num) {
        int result = -1;
        int left = 0;
        int right = ints.length - 1;

        while (left <= right) {
            int mid = (left + right) / 2;
            if (ints[mid] == num)
                return mid;
            if (ints[mid] < num) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return result;
    }


    private void loadData() {
        RetrofitHelper.getApi().getCategory().enqueue(new Callback<CategoryResponse>() {
            @Override
            public void onResponse(Call<CategoryResponse> call, Response<CategoryResponse> response) {
                tagList.clear();
                tagList.addAll(response.body().getDatas());
                currentTag = tagList.get(0);
                commonAdapter.notifyDataSetChanged();
                tagFlowLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        loadCourseListData(0);

                        View childAt = tagFlowLayout.getChildAt(0);
                        TextView tv_first = (TextView) childAt.findViewById(R.id.tv_title);
                        tv_first.setTextSize(18);
                        tv_first.setTextColor(Color.YELLOW);
                    }
                });
            }

            @Override
            public void onFailure(Call<CategoryResponse> call, Throwable t) {

            }
        });
    }

    private void init() {
        ll_container = findViewById(R.id.ll_container);
        tagFlowLayout = ((RecyclerView) findViewById(R.id.id_flowlayout));
        tv_name = (TextView) findViewById(R.id.tv_name);

        commonAdapter = new CommonAdapter<CategoryResponse.DatasBean>(this, R.layout.item_title, tagList) {
            @Override
            protected void convert(ViewHolder holder, CategoryResponse.DatasBean dataBean, int position) {
                ((TextView) holder.getView(R.id.tv_title)).setText(dataBean.getTp_name());
            }
        };
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                for (int i = 0; i < tagFlowLayout.getChildCount(); i++) {
                    TextView tv_title = (TextView) tagFlowLayout.getChildAt(i).findViewById(R.id.tv_title);
                    tv_title.setTextColor(Color.WHITE);
                    tv_title.setTextSize(14);
                    checkThread();
                }
                TextView tv_choose = (TextView) tagFlowLayout.getChildAt(position).findViewById(R.id.tv_title);
                tv_choose.setTextSize(18);
                tv_choose.setTextColor(Color.YELLOW);

                currentTag = tagList.get(position);
                loadCourseListData(position);
            }

            private void checkThread() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                    }
                }).start();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        tagFlowLayout.setAdapter(commonAdapter);
        tagFlowLayout.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        cardViewPager = ((ViewPager) findViewById(R.id.viewpager));
        strings = new ArrayList<>();
        this.adapter = new MainPagerAdapter(strings, MainActivity.this);

        cardViewPager.setAdapter(this.adapter);
        cardViewPager.setOffscreenPageLimit(10);
        cardViewPager.setPageMargin(0);
        cardViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv_name.setText(strings.get(position).getEd_name());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        cardViewPager.setPageTransformer(true, new ScaleInTransformer());
        ll_container.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("ffffff", "onTouch: x:" + event.getX() + "    y:  " + event.getY());
                return cardViewPager.onTouchEvent(event);
            }
        });


        cardViewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("ffffffs", "onClick: " + v.getClass().getSimpleName());
            }
        });
    }

    private void loadCourseListData(int position) {
        String deviceID = SystemUtil.getDeviceID(MainActivity.this);

        int tp_id = tagList.get(position).getTp_id();
        RetrofitHelper.getApi().getMainListResponse(tp_id, deviceID).enqueue(new Callback<MainListResponse>() {
            @Override
            public void onResponse(Call<MainListResponse> call, Response<MainListResponse> response) {

                strings.clear();
                MainListResponse body = response.body();
                if (body == null) {
                    Toast.makeText(MainActivity.this, "参数错误！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (body.getDatas() == null) {
                    Toast.makeText(MainActivity.this, "暂无数据！", Toast.LENGTH_SHORT).show();
                } else {
                    strings.addAll(body.getDatas());
                }
                cardViewPager.setAdapter(adapter);
                cardViewPager.setCurrentItem(strings.size() / 2);
            }

            @Override
            public void onFailure(Call<MainListResponse> call, Throwable t) {

            }
        });
    }

    public String getID() {
        return currentTag.getTp_id() + "";
    }
}
